package com.hmyd.hmyd_android.adapter;

import java.util.List;

import com.hmyd.hmyd_android.R;
import com.hmyd.hmyd_android.dto.Investment;
import com.hmyd.hmyd_android.utils.Utils;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.bitmap.BitmapCommonUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class InvestmentBillListAdapter extends BaseAdapter {

	private Context mContext;
	private List<Investment> cPlist;
	private LayoutInflater mInflater;

	BitmapUtils bitmapUtils;
	BitmapDisplayConfig displayConfig;

	public InvestmentBillListAdapter(Context context, List<Investment> plist) {
		super();

		bitmapUtils = new BitmapUtils(context);
		displayConfig = new BitmapDisplayConfig();
		displayConfig.setShowOriginal(false);
		displayConfig.setBitmapMaxSize(BitmapCommonUtils.getScreenSize(context));

		this.mContext = context;
		this.cPlist = plist;
		mInflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return cPlist != null ? cPlist.size() : 0;
	}

	@Override
	public Object getItem(int position) {
		return cPlist != null ? cPlist.get(position) : null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		if (cPlist == null || getItem(position) == null) {
			return convertView;
		}

		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();

			convertView = mInflater.inflate(R.layout.view_investment_newbie, null);
			
			holder.view_investment = (RelativeLayout) convertView.findViewById(R.id.view_investment);
			holder.grpj_view_investment = (RelativeLayout) convertView.findViewById(R.id.grpj_view_investment);
			
			//票据普通标
			holder.investInfo = (TextView) convertView.findViewById(R.id.investInfo);
			holder.smartWay = (TextView) convertView.findViewById(R.id.smartWay);
			holder.repayMethod = (TextView) convertView.findViewById(R.id.repayMethod);

			holder.rate = (TextView) convertView.findViewById(R.id.rate);
			holder.rate_sp = (TextView) convertView.findViewById(R.id.rate_sp);
			holder.rate_tag = (TextView) convertView.findViewById(R.id.rate_tag);

			holder.period = (TextView) convertView.findViewById(R.id.period);
			holder.minInvestAmount = (TextView) convertView.findViewById(R.id.minInvestAmount);

			holder.progress = (ProgressBar) convertView.findViewById(R.id.progress);
			holder.progress_label = (TextView) convertView.findViewById(R.id.progress_label);
			
			// 新手标
			holder.grpj_title = (TextView) convertView.findViewById(R.id.grpj_title);

			holder.grpj_investInfo = (TextView) convertView.findViewById(R.id.grpj_investInfo);
			holder.grpj_smartWay = (TextView) convertView.findViewById(R.id.grpj_smartWay);
			holder.grpj_repayMethod = (TextView) convertView.findViewById(R.id.grpj_repayMethod);

			holder.grpj_rate = (TextView) convertView.findViewById(R.id.grpj_rate);
			holder.grpj_rate_sp = (TextView) convertView.findViewById(R.id.grpj_rate_sp);
			holder.grpj_rate_tag = (TextView) convertView.findViewById(R.id.grpj_rate_tag);

			holder.grpj_period = (TextView) convertView.findViewById(R.id.grpj_period);
			holder.grpj_minInvestAmount = (TextView) convertView.findViewById(R.id.grpj_minInvestAmount);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		Investment investment = (Investment) getItem(position);

		if (null != investment) {
			if (investment.period == 7) {
				holder.view_investment.setVisibility(View.GONE);
				holder.grpj_view_investment.setVisibility(View.VISIBLE);
				
				Utils.changeTextSize(investment.investInfo, holder.grpj_investInfo, mContext.getResources().getColor(R.color.home_tab_text_pressed));
				Utils.changeTextSize(investment.smartWay, holder.grpj_smartWay, mContext.getResources().getColor(R.color.home_tab_text_pressed));
				Utils.changeTextSize(investment.repayMethod, holder.grpj_repayMethod, mContext.getResources().getColor(R.color.home_tab_text_pressed));

				getRateView(investment, holder);

				holder.grpj_title.setText("新人"+investment.period+"天标");
				
				holder.grpj_period.setText(investment.period+"天锁定期");
				holder.grpj_minInvestAmount.setText(investment.minInvestAmount+"元起投");

			} else {
				holder.view_investment.setVisibility(View.VISIBLE);
				holder.grpj_view_investment.setVisibility(View.GONE);
				
				Utils.changeTextSize(investment.investInfo, holder.investInfo, mContext.getResources().getColor(R.color.home_tab_text_pressed));
				Utils.changeTextSize(investment.smartWay, holder.smartWay, mContext.getResources().getColor(R.color.home_tab_text_pressed));
				Utils.changeTextSize(investment.repayMethod, holder.repayMethod, mContext.getResources().getColor(R.color.home_tab_text_pressed));

				getRateView(investment, holder);

				holder.period.setText(investment.period+"天");
				holder.minInvestAmount.setText(investment.minInvestAmount+"元");

				holder.progress.setProgress(Integer.parseInt(investment.progress));
				holder.progress_label.setText(investment.progress+"%");
			}
		}

		return convertView;
	}

	private void getRateView(Investment investment, ViewHolder holder){
		String rate_value = investment.rate;
		String rate = "";
		String rate_sp = "";
		String rate_tag = "";

		// 如果包含%， 删除所有%  手动添加
		rate_value = rate_value.contains("%") ? rate_value.replace("%", "") : rate_value;

		if (rate_value.contains("+")) {
			rate = rate_value.split("[+]")[0];
			rate_tag = rate_value.split("[+]")[1];

			if (rate.contains(".")) {
				rate_sp = rate.split("[.]")[1];
				rate = rate.split("[.]")[0]+".";
			}

			if (investment.period == 7) {
				holder.grpj_rate_tag.setVisibility(View.VISIBLE);
				
				holder.grpj_rate.setText(rate);
				holder.grpj_rate_sp.setText(rate_sp+"%");
				holder.grpj_rate_tag.setText("+"+rate_tag+"%");
			} else {
				holder.rate_tag.setVisibility(View.VISIBLE);
				holder.rate.setText(rate);
				holder.rate_sp.setText(rate_sp+"%");
				holder.rate_tag.setText("+"+rate_tag+"%");
			}
		} else {
			if (rate_value.contains(".")) {
				rate_sp = rate_value.split("[.]")[1];
				rate = rate_value.split("[.]")[0]+".";
			} else {
				rate = rate_value;
			}

			if (investment.period == 7) {
				holder.grpj_rate_tag.setVisibility(View.GONE);
				
				holder.grpj_rate.setText(rate);
				holder.grpj_rate_sp.setText(rate_sp+"%");
			} else {
				holder.rate_tag.setVisibility(View.GONE);
				holder.rate.setText(rate);
				holder.rate_sp.setText(rate_sp+"%");
			}
		}
	}

	class ViewHolder{
		RelativeLayout view_investment;
		RelativeLayout grpj_view_investment;
		
		/** 票据普通标  */
		// 头部特殊标签
		FrameLayout itemTagIconFrame;
		// 理财信息
		TextView investInfo;
		// 付息信息
		TextView smartWay;
		// 锁定信息
		TextView repayMethod;

		// 预期年化率 
		TextView rate;
		// 剩余
		TextView rate_sp;
		// 加息利率
		TextView rate_tag;

		// 锁定期
		TextView period;
		// 起投金额
		TextView minInvestAmount;

		// 时间进度
		ProgressBar progress;
		TextView progress_label;

		/** 票据新人标  */
		// title
		TextView grpj_title;
		// 理财信息
		TextView grpj_investInfo;
		// 付息信息
		TextView grpj_smartWay;
		// 锁定信息
		TextView grpj_repayMethod;

		// 预期年化率 
		TextView grpj_rate;
		// 剩余
		TextView grpj_rate_sp;
		// 加息利率
		TextView grpj_rate_tag;

		// 锁定期
		TextView grpj_period;
		// 起投金额
		TextView grpj_minInvestAmount;
	}
}
